package com.xinfu.newproject.ui.my_community;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.xinfu.newproject.R;
import com.xinfu.newproject.activity.LoginActivity;
import com.xinfu.newproject.adapter.my_community.MyCommunityAdapter;
import com.xinfu.newproject.base.BaseActivity;
import com.xinfu.newproject.bean.home.bean.GetAppIconBean;
import com.xinfu.newproject.bean.login.LoginBean;
import com.xinfu.newproject.ui.complaint_feedback.ComplaintsFragment;
import com.xinfu.newproject.util.BToast;
import com.xinfu.newproject.util.SPUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.xinfu.newproject.contants.Constant.LOGIN;

public class MyCommunityActivity extends BaseActivity {

    @BindView(R.id.activity_my_community_rv)
    RecyclerView mRecyclerView;

    MyCommunityAdapter myCommunityAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_community);
        ButterKnife.bind(this);
        registerBack();
        setActivityTitle("我的社区");
        initViewBind();
        initLisener();
    }

    private void initLisener() {

    }

    GetAppIconBean.ResultBean communityMenuBean;

    private void initViewBind() {
        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 4));
        myCommunityAdapter = new MyCommunityAdapter();
        communityMenuBean = new Gson().fromJson(getIntent().getExtras().getString("response"), GetAppIconBean.ResultBean.class);
        myCommunityAdapter.setNewData(communityMenuBean.getCommunityMenu());
        mRecyclerView.setAdapter(myCommunityAdapter);
        myCommunityAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                switch (communityMenuBean.getCommunityMenu().get(position).getCode()) {
                    case "0201":
                        break;
                    case "0202":
                        break;
                    case "0203":
                        break;
                    case "0204":
                        if (isLogin()) {
                            //投诉反馈
                            startIntentActivity(ComplaintsFragment.class, null);
                        } else {
                            startIntentActivity(LoginActivity.class, null);
                        }
                        break;
                }
            }
        });
    }

    /**
     * 判断是否可以请求数据
     */
    String token, userId, userName;

    private boolean isLogin() {

        LoginBean.ResultBean resultBean;
        String responseIntent = (String) SPUtils.get(this, LOGIN, "0");
        if (!responseIntent.equals("0")) {
            resultBean = new Gson().fromJson(responseIntent, LoginBean.ResultBean.class);
            token = resultBean.getToken();
            userId = resultBean.getUserInfo().getId();
            userName = resultBean.getUserInfo().getUsername();
        } else {
            BToast.showText(this, "请先登录");
            return false;
        }

        return true;
    }


}
